﻿#region
using System;
using System.Collections;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
#endregion

namespace YTBZ
{
    public partial class PagerNavigation : System.Web.UI.UserControl
    {
        public bool CurrPgSpit { get { return lBtnPgSpit.Visible; } } //是否启用分页模式

        #region Curr
        /// <summary>
        /// 当前页
        /// </summary>
        public int CurrPageIndex
        {
            get { return Convert.ToInt32(lblPageCurr.Text); }
            set { lblPageCurr.Text = value.ToString(); }
        } 
        /// <summary>
        /// 当前总页面数
        /// </summary>
        public int CurrPageAllCount
        {
            get { return lblPageAllCount.Text == "" ? 0 : Convert.ToInt32(lblPageAllCount.Text); }
            set { lblPageAllCount.Text = value.ToString(); }
        }
        /// <summary>
        /// 当前单页记录
        /// </summary>
        public int CurrPageOneRowCount
        {
            get { return lblPageCount.Text == "" ? 20 : Convert.ToInt32(lblPageCount.Text); }
            set { lblPageCount.Text = value.ToString(); }
        }
        /// <summary>
        /// 当前总记录数
        /// </summary>
        public int CurrPageAllRowCount
        {
            set { lBtnPgCurrRowCount.Visible = value > 0; lBtnPgCurrRowCount.Text = "共" + value.ToString() + "行"; }
        }
        #endregion

        #region Page_Load
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["MENUID"] != null && Request["MENUID"].ToString() != "")
                {
                    lBtnPgSet.CommandArgument = Request["MENUID"].ToString();
                    lBtnPgSet.Visible = true;
                }
            }
        }
        public delegate void BindDataDelegate();
        public BindDataDelegate BindData1;
        private GridView _dg;
        public void SetTarget(GridView adg, BindDataDelegate aBindData1)
        {
            BindData1 = aBindData1;
            _dg = adg;
        }
        public void SetStyle(string strPageCount)
        {
            if (lBtnPgSet.Visible && Session["PAGEROW_" + lBtnPgSet.CommandArgument] != null)
            {
                strPageCount = Session["PAGEROW_" + lBtnPgSet.CommandArgument].ToString(); 
            }

            lblPageCount.Text = strPageCount;
            _dg.AllowPaging = true;
            _dg.PagerSettings.Visible = false;
            int iCurrPageSize = CurrPageOneRowCount;
            if (iCurrPageSize == 0)
            {
                CurrPageOneRowCount = 0;
                iCurrPageSize = 20;
                CurrPageOneRowCount = iCurrPageSize;
            }
            txtOnePageRowCount.Value = iCurrPageSize.ToString();
            _dg.PageSize = iCurrPageSize;
            _dg.PagerSettings.Mode = PagerButtons.Numeric;
            _dg.PagerStyle.HorizontalAlign = HorizontalAlign.Right; 
        }
        #endregion

        #region lBtnPg
        protected void lBtnPgFirst_Click(object sender, EventArgs e)
        {
            int iShowPage = 0;
            CurrPageIndex = iShowPage;
            zxDataBinding(null, null);
        }

        protected void lBtnPgPervi_Click(object sender, EventArgs e)
        {
            int iShowPage = CurrPageIndex;
            iShowPage = (iShowPage > 0 ? iShowPage - 1 : 0);
            CurrPageIndex = iShowPage;
            zxDataBinding(null, null);
        }

        protected void lBtnPgNext_Click(object sender, EventArgs e)
        {
            int iShowPage = CurrPageIndex;
            if (iShowPage < CurrPageAllCount - 1)
            {
                iShowPage++;
            }
            CurrPageIndex = iShowPage;
            zxDataBinding(null, null);
        }

        protected void lBtnPgLast_Click(object sender, EventArgs e)
        {
            int iShowPage = (CurrPageAllCount == 0 ? 0 : CurrPageAllCount - 1);
            CurrPageIndex = iShowPage;
            zxDataBinding(null, null);
        }

        #endregion

        #region lBtnPgSet
        protected void lBtnPgGo_Click(object sender, EventArgs e)
        {
            int iShowPage = 0;
            try { iShowPage = Convert.ToInt32(tbPage.Text) - 1; }
            catch { return; }
            if (iShowPage <= CurrPageAllCount - 1 && iShowPage >= 0)
            {
                CurrPageIndex = iShowPage;
                zxDataBinding(null, null);
            }
        }
        protected void lBtnPgSet_Click(object sender, EventArgs e)
        {
            int iShowPageRow = 0;
            try { iShowPageRow = Convert.ToInt32(txtOnePageRowCountTT.Text) ; }
            catch { return; }
            //BasePage TT = new BasePage();
            if (iShowPageRow > 9 && iShowPageRow < 501)
            {
                //SortedList SDList = new SortedList();
                //SDList["V_PAGECOUNT"] = iShowPageRow;
                //SDList["V_KEYID"] = lBtnPgSet.CommandArgument;
                //SDList["V_OPTUSERID"] = TT.CURRUSERID;
                //SDList["V_OPTUSERNAME"] = TT.CURRUSERNAME;
                //string strBack = TT.proSetDT(SDList, "PRO_SYS.MENUSAVEPAGEROW");
                Session.Add("PAGEROW_" + lBtnPgSet.CommandArgument, iShowPageRow.ToString());
                SetStyle(iShowPageRow.ToString());
                zxDataBinding(null, null);
            }// else { TT.jsMessage("Please input 10 - 500"); }
        }
        #endregion

        #region 翻页事件
        private void zxDataBinding(object sender, System.EventArgs e)
        {
            if (!lBtnPgSpit.Visible)
            { 
                _dg.PageIndex = CurrPageIndex;
            }
            BindData1();
            lBtnPgCurr.Text = (CurrPageIndex + 1).ToString() + "/" + CurrPageAllCount + "页";
        }
        #endregion

        #region 数据绑定 (无分页) 
        public void proDataBind(int iPageRowCount, SortedList SDList, string DB_PackName)
        {
            if (_dg == null || _dg.Rows.Count == 0) { return; }
            lBtnExpAll.Enabled = true;

            if (_dg.Rows.Count < 1)
            {
                lBtnPgCurr.Text = "当前查询无记录"; 
                lBtnExpAll.Enabled = false;
                return;
            }
            CurrPageAllCount = _dg.PageCount;
            if (CurrPageIndex + 1 > CurrPageAllCount) { CurrPageIndex = 0; }
            lBtnPgCurr.Text = (CurrPageIndex + 1).ToString() + "/" + CurrPageAllCount + "页";
            _dg.PageIndex = CurrPageIndex;

            if (iPageRowCount > 0)
            {
                CurrPageAllRowCount = iPageRowCount;
                Session.Add(DB_PackName, SDList);
                lblPackName.Text = DB_PackName;
                lBtnExpAll.Visible = true;
            }
            else
            {
                lBtnExpAll.Visible = false;
            }
        }
        
        #endregion

        #region 数据绑定 (分页模式) 
        public void proDataBind(int iPageRowCount,int iCurrPage, SortedList SDList, string DB_PackName)
        {
            try
            {
                lBtnPgSpit.Visible = true; //分页模式
                if (_dg == null || _dg.Rows.Count == 0) { return; }
                lBtnExpAll.Enabled = true;

                if (_dg.Rows.Count < 1)
                {
                    lBtnPgCurr.Text = "当前查询无记录";
                    lBtnExpAll.Enabled = false;
                    return;
                } 
                CurrPageAllCount = (iPageRowCount + CurrPageOneRowCount - 1) / CurrPageOneRowCount;
                lBtnPgCurr.Text = (iCurrPage==0?"1": iCurrPage.ToString()) + "/" + CurrPageAllCount.ToString() + "页";
                _dg.PageIndex = CurrPageIndex; 
                if (iPageRowCount > 0)
                {
                    CurrPageAllRowCount = iPageRowCount;
                    Session.Add(DB_PackName, SDList);
                    lblPackName.Text = DB_PackName;
                    lBtnExpAll.Visible = true;
                }
                else
                {
                    lBtnExpAll.Visible = false;
                }
            }
            catch (Exception Ex)
            {
                BasePage TT = new BasePage();
                TT.proSaveErro(Ex.ToString());
            }
        }
        /// <summary>
        /// 数据绑定，但不显示导出按钮
        /// </summary>
        /// <param name="iPageRowCount"></param>
        /// <param name="iCurrPage"></param>
        public void proDataBind(int iPageRowCount, int iCurrPage)
        {
            try
            {
                lBtnPgSpit.Visible = true; //分页模式
                if (_dg == null || _dg.Rows.Count == 0) { return; }
                //lBtnExpAll.Enabled = true;

                if (_dg.Rows.Count < 1)
                {
                    lBtnPgCurr.Text = "当前查询无记录";
                    lBtnExpAll.Enabled = false;
                    return;
                }
                CurrPageAllCount = (iPageRowCount + CurrPageOneRowCount - 1) / CurrPageOneRowCount;
                lBtnPgCurr.Text = (iCurrPage == 0 ? "1" : iCurrPage.ToString()) + "/" + CurrPageAllCount.ToString() + "页";
                _dg.PageIndex = CurrPageIndex;
                if (iPageRowCount > 0)
                {
                    CurrPageAllRowCount = iPageRowCount;
                    //Session.Add(DB_PackName, SDList);
                    //lblPackName.Text = DB_PackName;
                    //lBtnExpAll.Visible = true;
                }
                else
                {
                    lBtnExpAll.Visible = false;
                }
            }
            catch (Exception Ex)
            {
                BasePage TT = new BasePage();
                TT.proSaveErro(Ex.ToString());
            }
        }
        #endregion

        #region 导出
        protected void lBtnExpAll_Click(object sender, EventArgs e)
        {
            DataTable dtExp = getExpDataSouse();
            if (dtExp != null)
            {
                #region 增加导出日志
                string strTitle = Request["TITLE"].ToString();
                string strMode = strTitle.Substring(strTitle.LastIndexOf(">") + 1);
                BasePage TT = new BasePage();
                TT.proREG("导出数据", strMode, strTitle + "页面导出数据！",
                       null, null, null, null);
                #endregion

                ExceptExcelAll(dtExp, _dg);
            }
        }
        protected DataTable getExpDataSouse()
        {
            SortedList SLParamsTT = (SortedList)Session[lblPackName.Text];
            if (lBtnPgSpit.Visible) {  //分页需特殊处理
                foreach (DictionaryEntry element in SLParamsTT) 
                {
                    if (element.Key.ToString().ToUpper() == "V_CURRPAGESIZE")
                    {
                        SLParamsTT["V_CURRPAGESIZE"] = 65000; break;
                    } 
                }
                foreach (DictionaryEntry element in SLParamsTT)
                { 
                    if (element.Key.ToString().ToUpper() == "V_CURRPAGE")
                    {
                        SLParamsTT["V_CURRPAGE"] = 0;break;
                    }
                }
            }
             
            DB TT = new DB();
            DataTable dtExp = TT.proGetDT(SLParamsTT, lblPackName.Text);
            return dtExp;
        }
        /// <summary>
        /// 导出全部数据
        /// </summary>
        /// <param name="dt">原始数据</param>
        /// <param name="gv">显示格式</param>
        protected void ExceptExcelAll(DataTable dt, GridView gv)
        {
            try
            {
                GridView gvOut = new GridView();
                gvOut.AutoGenerateColumns = false;
                gvOut.AllowPaging = false;
                gvOut.CssClass = gv.CssClass;
                gvOut.HeaderStyle.CssClass = gvOut.HeaderStyle.CssClass;
                gvOut.RowStyle.CssClass = gvOut.RowStyle.CssClass;
                gvOut.AlternatingRowStyle.CssClass = gvOut.AlternatingRowStyle.CssClass;
                for (int i = 0; i < gv.Columns.Count; i++)
                {
                    if (gv.Columns[i].GetType().Name == "BoundField" && gv.Columns[i].HeaderText != "")  //肯定是可以显示的数据帮定字段 BoundField HeaderText="" 可以规避隐藏列
                    {
                        BoundField bf = new BoundField();
                        bf.HeaderText = gv.Columns[i].HeaderText;
                        bf.DataField = ((BoundField)gv.Columns[i]).DataField;
                        gvOut.Columns.Add(bf);
                    }
                }

                #region  增加储位单信息
                if (lblPackName.Text == "PRO_GOODIN.GOODINITEMQUERYBYPAGE") {
                    DB TT = new DB();
                    BoundField bf = new BoundField();
                    bf.HeaderText = "储位详情";
                    bf.DataField = "TT_CWXQ";
                    gvOut.Columns.Add(bf);
                    dt.Columns.Add("TT_CWXQ");

                    foreach (DataRow dr in dt.Rows)
                    {
                        SortedList SDSpace = new SortedList();
                        SDSpace["V_ITEMID"] = dr["KEYITEMID"].ToString();
                        DataTable dtSpace = TT.proGetDT(SDSpace, "PRO_GOODIN.GETSPACEREGBYITEM");

                        string strInfo = "";
                        foreach (DataRow drC in dtSpace.Rows)
                        {
                            strInfo += drC["SPACECODE"].ToString() + " " + drC["STORENUMBER"].ToString() + " " + Environment.NewLine;
                        }
                        dr["TT_CWXQ"] = strInfo;
                    } 
                }
                #endregion

                gvOut.DataSource = dt;
                gvOut.DataBind();
                foreach (GridViewRow dr in gvOut.Rows)
                {
                    foreach (TableCell tc in dr.Cells)
                    {
                        tc.Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                    }
                }
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "GB2312";
                string expFileName = "attachment;filename=" + "XLS" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
                //if (rExecTypeDoc.Checked)
                //{
                //    expFileName = "attachment;filename=" + "DOC" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".doc";
                //}
                Response.AppendHeader("Content-Disposition", expFileName);
                Response.ContentType = "application/ms-excel";
                this.EnableViewState = false;
                System.IO.StringWriter swOut = new System.IO.StringWriter();
                HtmlTextWriter hTw = new HtmlTextWriter(swOut);
                gvOut.RenderControl(hTw);
                Response.Write(swOut.ToString());
                Response.End();
            }
            catch (Exception ex)
            {
                base.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=javascript>alert('导出数据失败:" + ex.ToString() + "');</script>");
            }
        }
        #endregion
    }
}